package com.sihan.swvp.service.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.sihan.swvp.dao.sys.UserDao;
import com.sihan.swvp.entity.sys.Enterprise;
import com.sihan.swvp.entity.sys.User;
import com.sihan.swvp.entity.sys.UserConfig;

@Component
public class UserService {
	@Autowired
	UserDao userDao;

	public User getUser(String account, String password) {
		// TODO Auto-generated method stub
		User user = userDao.getUserByPwd(account,password);
		if(user != null){
			userDao.addCounts(user.getId().toString(),userDao.getUserLogCounts(user.getId().toString()));
		}
		return user;
		
	}

	public void addUser(User user) {
		// TODO Auto-generated method stub
		userDao.adduser(user);
	}

	public List<Map<String, Object>> getUser(Map<String,Object> params) {
		// TODO Auto-generated method stub
		return userDao.getUser(params);
	}
	
	public User getUserById(User user) {
		// TODO Auto-generated method stub
		return userDao.getUserById(user);
	}

	public void udpateUser(User user) {
		// TODO Auto-generated method stub
		userDao.udpateUser(user);
	}

	public void deleteUser(User user,String ids) {
		// TODO Auto-generated method stub
		userDao.deleteUser(user.getId(),ids);
	}

	public List<Map<String, Object>> getEnterprise() {
		// TODO Auto-generated method stub
		return userDao.getEnterprise();
	}
	
	
	public Enterprise getUserEnterprise(User user) {
		// TODO Auto-generated method stub
		return userDao.getUserEnterprise(user);
	}
	
	public UserConfig getConfig(User user) {
		List<Map<String,Object>> config = userDao.getConfig(user);
		
		return new UserConfig(config);
	}

	public void saveConfig(HttpSession session,JSONObject jcfg) {
		Set<String> keyset = jcfg.keySet();
		List<Map<String,Object>> config = new ArrayList<Map<String,Object>>();
		for (String key : keyset) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("key", key);
			map.put("value", jcfg.get(key));
			config.add(map);
		}
		
		userDao.saveConfig(((User)session.getAttribute("u")).getId(),config);
		
		UserConfig uconfig = (UserConfig)session.getAttribute("config");
		uconfig.update(config);
	}
	
	
	public List<User> getUserNameCount(String account){
		return userDao.getUserByAccount(account);
	}
	
	public int userNameIsExistence(String account,Long id){
		int n = userDao.userNameIsExistence(account,id);
		return n;
	}
	
	public int adminPriv(Long userId){
		return userDao.adminPriv(userId);
	}
	
	public String getUserLoginCount(String userid){
		return userDao.getUserLogCounts(userid);
	}
	
	public void setUser(Map<String,Object> params) {
		userDao.setUser(params);
	}
	
	public User selectUserByAccount(String account){
		return userDao.selectUserByAccount(account);
	}

	public void updateLoginErrorCount(Long id,int newerrorcount) {
		userDao.updateLoginErrorCount(id,newerrorcount);
	}
	
	public void updateUserLocked(Long id){
		userDao.updateUserLocked(id);
	}
	
	public void updateUserRemoveLocked(Long id){
		userDao.updateUserRemoveLocked(id);
	}
	
	public void updateConn(Long id,int isconn){
		userDao.updateConn(id, isconn);
	}
	
	public void setConnById(Long id,int isconn){
		userDao.setConnById(id, isconn);
	}
	
	public int getConnCount(){
		return userDao.getConnCount();
	}
}
